﻿using System.Windows;
using System;
using System.Windows.Controls.Primitives;
using Size = System.Windows.Size;

namespace BloodApp.Common
{
    /// <summary>
    /// UniformGrid布局：先从上往下，在从左往右
    /// </summary>
    public class VerticalUniformGrid : UniformGrid
    {
        protected override Size ArrangeOverride(Size arrangeSize)
        {
            int rowCount = Rows > 0 ? Rows : (int)Math.Ceiling(Math.Sqrt(InternalChildren.Count));
            int columnCount = Columns > 0 ? Columns : (int)Math.Ceiling(InternalChildren.Count / (double)rowCount);

            double rowHeight = arrangeSize.Height / rowCount;
            double columnWidth = arrangeSize.Width / columnCount;

            int currentRow = 0;
            int currentColumn = 0;

            foreach (UIElement child in InternalChildren)
            {
                double x = currentColumn * columnWidth;
                double y = currentRow * rowHeight;

                child.Arrange(new Rect(x, y, columnWidth, rowHeight));

                if (++currentRow >= rowCount)
                {
                    currentRow = 0;
                    currentColumn++;
                }
            }

            return arrangeSize;
        }
    }
}
